---
--- 字符串工具类
--- Created by E.T.
--- DateTime: 2018/11/9 11:11
---
_M={}

---从右侧遍历字符串，取指定字符的前后字符串
-- @param strurl  待解取字符串；
--        strchar 指定字符串；
--        bafter= true 取指定字符后字符串
-- @return 截取后的字符串
-- end --
function _M.get_url_string( strurl, strchar, bafter)
    local ts = string.reverse(strurl)
    local param1, param2 = string.find(ts, strchar)  -- 这里以"/"为例
    local m = string.len(strurl) - param2 + 1
    local result
    if (bafter == true) then
        result = string.sub(strurl, m+1, string.len(strurl))
    else
        result = string.sub(strurl, 1, m-1)
    end
    return result
end



-- 字符串前缀匹配
-- 返回 true or false
function _M.match(strurl,whiteListUrl)
    local index = string.find(whiteListUrl,"/**")
    if(nil ~= index and index > 0) then
        local substr = string.sub(whiteListUrl,0,index);
        if(substr ~= nil and string.match(strurl,substr .. "[%w_]")) then
            return true;
        end
    end
    return false;
end


--字符串截取，返回table
function _M.split(str,split_char)
    local resultStrList = {}
    if str == nil or str == "" or split_char == nil then
        return nil
    end
    string.gsub(str,'[^'..split_char..']+',function ( w )
        table.insert(resultStrList,w)
    end)
    return resultStrList
end

--按符号移除第一个元素
function _M.remove_path_first(path,split_char)
    local result ;
    local t = _M.split(path,split_char)
    table.remove(t,1)
    result = split_char..table.concat(t,split_char);
    return result
end

--16进制转2进制
function _M.hex2bin(hexstr)
    local str = ""
    for i = 1, string.len(hexstr) - 1, 2 do
        local doublebytestr = string.sub(hexstr, i, i+1);
        local n = tonumber(doublebytestr, 16);
        if 0 == n then
            str = str .. '\00'
        else
            str = str .. string.format("%c", n)
        end
    end
    return str
end

return _M